package main

import (
	"encoding/json"
	"fmt"
	"github.com/atotto/clipboard"
	"github.com/go-vgo/robotgo"
	"os"
	"strconv"
	"time"
)

type Text struct {
	Msg string
}

func main() {
	filePtr, err := os.Open("data.json")
	if err != nil {
		fmt.Println("打开文件失败")
		return
	}
	defer filePtr.Close()

	var text []Text

	decoder := json.NewDecoder(filePtr)
	err = decoder.Decode(&text)

	if err != nil {
		panic("读取文件失败")
	}

	fmt.Println("读取完成")

	// 读取参数
	timeStop := os.Args[1]
	// 默认一秒一喷
	if len(timeStop) == 0 {
		timeStop = strconv.Itoa(1000)
	}

	timeLimit, _ := strconv.Atoi(timeStop)

	for i := 5; i > 0; i-- {
		fmt.Println("开喷倒计时..." + strconv.Itoa(i))
		time.Sleep(1 * time.Second)
	}

	for _, v := range text {
		clipboard.WriteAll(v.Msg)

		//robotgo.MoveMouse(1024, 780)

		//robotgo.MouseClick(`left`, false)

		robotgo.KeyTap("enter")
		robotgo.KeyTap("v", "control")
		robotgo.KeyTap("enter")

		time.Sleep(time.Millisecond * time.Duration(timeLimit))
	}

	fmt.Println("喷完收工")
}
